// pages/user_repair/user_repair.js
const $ = require('../../utils/utils');
const config = require('../../config');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        repairTyeps: [
            { "text": "无法开门", "code": 1 },
            { "text": "无法关门", "code": 2 },
            { "text": "图书破损", "code": 3 },
            { "text": "书柜损坏", "code": 4 },
            { "text": "结算错误", "code": 5 },
            { "text": "书籍数量", "code": 6 },
            { "text": "借阅费用", "code": 7 },
            { "text": "其 他", "code": 8 },
        ],
        pictures:[],
        fullNames:[],
        typeItemNor: 'type-item-nor',
        typeItemSel: 'type-item-sel',
        sel_type: { "text": "无法开门", "code": 1 },
        remarkNote: "",
        shelfCode: "",
        grid: "",
        isSuccess: false,
        maintain_type:"",
        path:""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        $.hideLoading()
        let grid = wx.getStorageSync("grid_code")
        let shelfCode = wx.getStorageSync("bookShelfCode")
        if (options.maintain_type == 20){
            wx.setStorageSync("isBorrowing_Repair", true)
        }

        
        this.setData({
            shelfCode: shelfCode,
            grid: grid,
            maintain_type: options.maintain_type,
            path: options.path
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    onUnload:function(){
        wx.removeStorageSync("isBorrowing_Repair")
    },

    selectRepairType:function(el){
        
        this.setData({
            sel_type:el.target.dataset.type 
        })
        
    },

    choosePicture: function(){
        let that = this
        let pictures = Array.from(this.data.pictures) 
        wx.chooseImage({
            count:4,
            sizeType: ["compressed"],
            success: function(res) {
                that.setData({
                    pictures: pictures.concat(res.tempFilePaths)
                })

                res.tempFilePaths.forEach(path=>{
                    console.log(path)
                    $.uploadToOSS(path).then(data => {
                        let fullNames = Array.from(that.data.fullNames)
                        fullNames.push(data)
                        that.setData({
                            fullNames: fullNames
                        })
                    }).catch(error => {

                    })
                })
                
            },
        })
    },

    deletImageUrl:function(el){
        let pictures = Array.from(this.data.pictures) 
        let fullNames = Array.from(this.data.fullNames)
        pictures.splice(el.currentTarget.dataset.index,1)
        this.setData({
            pictures: pictures
        })
    },

    changeRemark: function (text) {
        console.log(text)
        this.setData({
            remarkNote: text.detail.value
        })
    },

    tapConfirmAction: function () {
        var that = this
        let code = that.data.sel_type.code
        let remark = that.data.remarkNote
        let names = new Set(that.data.fullNames)
        let fullNames = Array.from(names).join()
        let params = {
            code: that.data.sel_type.code,
            describe: that.data.remarkNote,
            userid: wx.getStorageSync("userid"),
            usertoken: wx.getStorageSync("usertoken"),
            maintain_type: that.data.maintain_type,
            imgUrls: fullNames
        }

        $.post(config.reportAbnormal,params).then(data=>{
            if(data.code == 0){
                $.confirm("提示", "报修成功", function () {
                    wx.closeSocket();
                    wx.onSocketClose(function (res) {
                        console.log('WebSocket 已关闭！');
                        if (that.data.path == "borrow_success") {
                            wx.navigateBack({
                            })
                        } else {
                            wx.reLaunch({
                                url: config.page.index
                            })
                        }
                    })
                })
                
            }else{
                $.confirm("提示",data.message, function(){
                    wx.navigateBack({
                    })
                })

            }
            
        }).catch(error => {
           
        })
    }


    


})